package com.example.ui.register;

import java.util.ArrayList;
import java.util.List;

import com.example.huashangapp.R;
import com.example.util.ArrayWheelAdapter1;
import com.example.util.OnWheelChangedListener;
import com.example.util.Util;
import com.example.util.WheelView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity4 extends Activity {

	private Context context;
	public EditText ra4_name;
	private Button ra4_btn_selectgender;
	private ImageView ra4_txtclear;
	private LinearLayout ra4_l_selectgender;
	private RelativeLayout ra4, ra4_l_gender, ra4_l_city;
	WheelView gender;
	int n_proID;
	private List<String> gs;
	private TextView ra4_gender;
	private static TextView ra4_city;

	public static String provinceID;
	
	public static String province = "";
	public static String city;
	public static  String g="";
	public static String name = "";
	public static String password="";
	
	private TextView ra4_title;
	private Button ra4_next, ra4_return;
	public static Handler h;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		context=this;
		setContentView(R.layout.a_register4);
		init();
		super.onCreate(savedInstanceState);
	}
	public void init() {
		ra4_title = (TextView) findViewById(R.id.register_title);
		ra4_next = (Button) findViewById(R.id.register_next);
		ra4_return = (Button) findViewById(R.id.register_return);
		ra4_title.setText("个人信息(4/5)");
		password=getIntent().getStringExtra("password");
		
		ra4_next.setOnClickListener(ra4_ocl);
		ra4_return.setOnClickListener(ra4_ocl);
		ra4_txtclear = (ImageView) findViewById(R.id.ar4_clear);
		ra4_l_city = (RelativeLayout)findViewById(R.id.ar4_l_city);
		ra4_l_gender = (RelativeLayout)findViewById(R.id.ar4_l_gender);

		ra4_l_selectgender = (LinearLayout) 
				findViewById(R.id.ar4_selectgender);
		ra4_btn_selectgender = (Button) 
				findViewById(R.id.ar4_btn_selectgender);

		ra4 = (RelativeLayout)findViewById(R.id.ar4);
		ra4_name = (EditText) findViewById(R.id.ar4_name);

		ra4_gender = (TextView) findViewById(R.id.ar4_btn_gender);
		ra4_city = (TextView) findViewById(R.id.ar4_btn_city);
		h = new Handler() {
			@Override
			public void handleMessage(Message msg) {						
			  ra4_city.setText(province+"\n"+city);                  									
				super.handleMessage(msg);
			}
		};
		gender = (WheelView) findViewById(R.id.gender);
		initGender();
		ra4_txtclear.setOnClickListener(ra4_ocl);
		ra4.setOnClickListener(ra4_ocl);
		ra4_l_city.setOnClickListener(ra4_ocl);
		ra4_l_gender.setOnClickListener(ra4_ocl);
		ra4_l_gender.setFilterTouchesWhenObscured(true);
		ra4_btn_selectgender.setOnClickListener(ra4_ocl);
	}
	
	
	
	

	private void initGender() {

		gs = new ArrayList<String>();
		gs.add("男");
		gs.add("女");
		gender.setAdapter(new ArrayWheelAdapter1(gs));
		gender.addChangingListener(new gListener());
	}

	OnClickListener ra4_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.register_return:
				//((Activity) context).onBackPressed();
				new AlertDialog.Builder(context).setTitle("提示").setMessage("是否返回重新验证手机号码")
				.setNegativeButton("否", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				})
				.setPositiveButton("是",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent i=new Intent();
						i.setClass(context, RegisterActivity01.class);
						startActivity(i);
						finish();
					}
				}).show();
				
				break;
			case R.id.register_next:
				name = ra4_name.getText().toString();
				
				if (!name.equals("")) {
					if (!g.equals("")) {
						if (!province.equals("")) {
							Intent i=new Intent();
							i.setClass(context, RegisterActivity05.class);
							startActivity(i);
							finish();
						}
					}
				}

				break;

			case R.id.ar4_clear:
				ra4_name.setText("");
				break;
			// 性别选择器的完成按钮
			case R.id.ar4_btn_selectgender:

				if (g.equals("")) {
					g = "男";
					ra4_gender.setText(g);
				} else {
					ra4_gender.setText(g);
				}

				ra4_l_selectgender.setVisibility(View.GONE);
				break;

			// 页面大布局
			case R.id.ar4:
				ra4_l_selectgender.setVisibility(View.GONE);
				break;
			// 城市
			case R.id.ar4_l_city:
				Intent i=new Intent();
				i.setClass(context, SelectPlaceFragment.class);
				startActivity(i);
				break;
			// 性别
			case R.id.ar4_l_gender:
				InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); 
				imm.hideSoftInputFromWindow(ra4_name.getWindowToken(), 0);
				ra4_l_selectgender.setVisibility(View.VISIBLE);
				break;

			default:

				break;
			}
		}
	};
	
	

	private class gListener implements OnWheelChangedListener {
		@Override
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			g = gs.get(newValue);
		}
	}
}
